﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: mshield
 * Datum: 15.04.2009
 * Zeit: 18:38
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */
using System;
using System.Collections.Generic;

namespace SarsClient
{
	/// <summary>
	/// This class contains all information about a buddy list. 
	/// </summary>
	public class BuddyList
	{
		Dictionary<string, Peer> nameToPeer;
		
	
		//Constructor
		public BuddyList()
		{
			nameToPeer = new Dictionary<string, Peer>();
		}
	
		//adds a new buddy, if already exists NO overwrite
		public void addBuddy(string name, Peer peer)
		{
			if (!nameToPeer.ContainsKey(name))
			{
				nameToPeer.Add(name, peer);
			}
		}
	
		//removes a buddy from the list by name
		public void removeBuddy(string name)
		{
			nameToPeer.Remove(name);
		}
		
		//will change the name = key of a buddy
		public void renameBuddy(string name, string newName)
		{
			if (nameToPeer.ContainsKey(name))
			{
				Peer peer = nameToPeer[name];
				nameToPeer.Remove(name);
				nameToPeer.Add(newName, peer);
			}
		}
		
		//will change the information(peer) = value of a buddy
		public void changeBuddyInformation(string name, Peer peer)
		{
			if (nameToPeer.ContainsKey(name))
			{
				nameToPeer.Remove(name);
				
				addBuddy(name, peer);
			}
		}
		
		//returns a list of all buddy names, useful for UI
		public List<string> getBuddyNames()
		{
			return new List<string>(this.nameToPeer.Keys);
		}
		
		//returns a Peer by name, useful for connecting to a buddy
		public Peer getBuddyInformation(string name)
		{
			if (nameToPeer.ContainsKey(name))
			{
				return nameToPeer[name];
			}
			else
			{
				return null;
			}
			
		}
		
		//debug to String function for the buddy list
		public override string ToString()
		{
			string ret = "";
			
			foreach(string name in nameToPeer.Keys)
			{
				string s = name + ", " + nameToPeer[name].Id.ToString() + ", " + nameToPeer[name].PeerIP.ToString() + "\n";
				Logging.Logger.DebugFormat(s);
				ret += s;
			}
			return ret;

		}
	
	}
	
	
	
	
	
}
